package jp.co.toshiba.ffsa.action.csde;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.common.Crypto;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.entity.Workflow;
import jp.co.toshiba.ffsa.form.csde.Csde701Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csde.Csde701Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * ES出荷確認画面
 * @author well-moon
 *
 */
public class Csde701Action extends CommonAction{

    protected String screenID = "CSDE701";

    @ActionForm
    @Resource
    protected Csde701Form csde701Form;

    @Resource
    protected Csde701Service csde701Service;

    /**
     * 初期処理
     * @return 画面遷移先
     */
    @Execute(validator = false)
    public String index() throws Exception {

        try {
            // ログイン済判定する
            if( !judgeLogin(csde701Form.getSessionId()) ){
                return topTransition;
            }

            // リファラ判定
            if( !judgeRefferer() ){
                return topTransition;
            }

            return "/csde/csde701.jsp";

        } catch (Exception e) {
            errorLogWriter(e, "index", screenID);
            throw e;
        }


    }

    /**
     * バリデーションエラー処理
     * @return 遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String error() throws Exception {
        return index();
    }

    /**
     * 更新処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true, input="/csde/csde701/error")
    public String updateRegInfo() throws Exception {
        String transition = "/csde/csde701.jsp"; // 遷移先
        String result = "";


        try {
            // ログイン済判定する
            if( !judgeLogin(csde701Form.getSessionId()) ){
                return topTransition;
            }

            // リファラ判定
            if( !judgeRefferer() ){
                return topTransition;
            }

            // 暗号化されていた機密情報を復号
            decryptSecretData();

            // 値をセットする
            csde701Service.setUserID(((LoginDto) RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());
            csde701Service.setProjectId(csde701Form.projectId);
            csde701Service.setWorkflowId(csde701Form.workflowId);
            csde701Service.projectName = csde701Form.projectName;

            // Workflowテーブルの情報取得
            Workflow workflow = csde701Service.getWorkflowInfo();

            if(workflow != null){
                // 更新処理の結果件数が1の場合
                int updateCount = csde701Service.UpdateEsStatus();

                // 更新処理の結果件数が1の場合
                if(updateCount == 1){
                    transition = "/csde/csde301";
                    result = ConstantValue.success;

                // 更新処理の結果件数が1以外の場合
                }else{
                    ActionMessages messages = new ActionMessages();
                    messages.add("updateFailed", new ActionMessage("csde701.006", true));
                    ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                }
            // 該当するデータが存在しない場合
            }else{
                ActionMessages messages = new ActionMessages();
                messages.add("searchFailed", new ActionMessage("csde701.005", true));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
            }

            //  メール送信
            if( !csde701Service.mailSend() ){
                ActionMessages messages = new ActionMessages();
                messages.add("mailSendFailed", new ActionMessage(csde701Service.errorCode, true));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
            }

            if(ConstantValue.success.equals(result)){
                // 操作ログ記述
                result = ConstantValue.success;
                ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                ArrayList<String> parameterNames = new ArrayList<String>();
                ArrayList<String> values = new ArrayList<String>();
                parameterNames.add(MessageResourcesUtil.getMessage("csde701.002"));
                values.add(csde701Form.confirmed_shipping_date);
                parameters.add(parameterNames);
                parameters.add(values);
                entryOperationHistory(screenID, "register", parameters, result);
            }

            // 機密情報を暗号化
            encryptSecretData();

            return transition;

        } catch (Exception e) {
            errorLogWriter(e, "updateRegInfo", screenID);
            throw e;
        }

    }

    private void encryptSecretData() throws Exception {
        csde701Form.projectId = Crypto.encrypt(csde701Form.projectId);
    }

    private void decryptSecretData() throws Exception {
        csde701Form.projectId = Crypto.decrypt(csde701Form.projectId);
    }

}